<?php
namespace Admin;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session ;
use Illuminate\Support\Facades\Validator ;
use Illuminate\Support\Facades\Redirect ;

class PostController extends BaseController
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //$posts = \Post::all()->paginate(5);
        //if ( !empty($ptid) ) $posts = \Post::where('ptid', '=', $ptid)->with('postType')->paginate(7);
        //else $posts = \Post::with('postType')->paginate(7);
        $posts = \Post::with('postType')->orderBy('created_at', 'desc')->paginate(7);
        
        return View::make('admin.Post.index')->with('posts', $posts) ;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('admin.Post.create') ;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'title'       => 'required',
            'body'      => 'required',
            'ptid' => 'required'
        );
        $validator = Validator::make(Input::all(), $rules);
        
        // process the login
        if ($validator->fails()) {
            return Redirect::to('admin/post/create')->withErrors($validator);
        } else {
            
            $destinationPath = '';
            $filename        = '';
            
            if (Input::hasFile('thumb')) {
                $file            = Input::file('thumb');
                $destinationPath = public_path() . '/upload/' ;
                $extension = $file->getClientOriginalExtension();
                //$filename        = str_random(6) . '_' . $file->getClientOriginalName();
                $filename        = str_random(15) . '.' . $extension;
                $uploadSuccess   = $file->move($destinationPath, $filename);
            }
            
            // store
            $post = new \Post();
            $post->title        =   Input::get('title');
            $post->keywords     =   Input::get('keywords');
            $post->description  =   Input::get('description');
            $post->hits         =   Input::get('hits');
            $post->thumb        =   $filename ;
            $post->body         =   Input::get('body');
            $post->ptid         =   Input::get('ptid');
            
            $pid = $post->save(); 
            
            /* $pid = \Post::create(
                [
                    'title' =>  Input::get('title') ,
                ]
            ); */
            
            if ($pid) {
                // redirect
                Session::flash('message', ' 日志添加OK！');
                return Redirect::to('admin/post');
            }else{
                Session::flash('message', '日志添加失败！');
                return Redirect::to('admin/post/create');
            }
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param int $id            
     * @return Response
     */
    public function show($id)
    {
        $post = \Post::find($id);
        return View::make('admin.Post.show')->with('post', $post) ;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id            
     * @return Response
     */
    public function edit($id)
    {
        $post = \Post::find($id);
        return View::make('admin.Post.edit')->with('post', $post) ;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id            
     * @return Response
     */
    public function update($id)
    {
         // validate
		// read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'title'       => 'required',
            'body'      => 'required',
            'ptid' => 'required'
        );
		$validator = Validator::make(Input::all(), $rules);

		// process the login
		if ($validator->fails()) {
			return Redirect::to('admin/post/' . $id . '/edit')->withErrors($validator);
		} else {
		    
		    $destinationPath  =   '';
		    $filename         = '';
		    
		    if (Input::hasFile('thumb')) {
		        $file            = Input::file('thumb');
		        $destinationPath = public_path() . '/upload/' ;
		        $filename        = str_random(6) . '_' . $file->getClientOriginalName();
		        $uploadSuccess   = $file->move($destinationPath, $filename);
		    }
		    
			// store
			$post = \Post::find($id);
			
            $post->title        =   Input::get('title');
            $post->keywords     =   Input::get('keywords');
            $post->description  =   Input::get('description');
            $post->hits         =   Input::get('hits');
            $post->thumb        =   !empty($filename) ? $filename : Input::get('othumb') ;
            $post->body         =   Input::get('body');
            $post->ptid         =   Input::get('ptid');
            
			$post->save();
			
			// redirect
			Session::flash('message', '日志更新OK！');
			return Redirect::to('admin/post');
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id            
     * @return Response
     */
    public function destroy($id)
    {

		$post = \Post::find($id);
		$post->delete();

		// redirect
		Session::flash('message', '['.$post->title.'] - 已删除！');
		return Redirect::to('admin/post');
    }
}
